package quq.missq.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.easemob.EMCallBack;

import java.util.HashMap;
import java.util.Map;

import quq.missq.BaseActivity;
import quq.missq.Constants;
import quq.missq.MissQApplication;
import quq.missq.R;
import quq.missq.beans.MyInformationBean;
import quq.missq.huanxin.ChatHistoryActivity;
import quq.missq.utils.BitmapUtils;
import quq.missq.utils.SharePreUtils;
import quq.missq.utils.UserTools;
import quq.missq.utils.VolleyTool;

/**
 * Created by River on 8/17/15.
 */
public class MyInformationActivity extends BaseActivity implements View.OnClickListener,
        VolleyTool.HTTPListener {

    private RelativeLayout mHome;
    private RelativeLayout mMyPhoto;
    // private RelativeLayout mSugar;
    private RelativeLayout mAtMe;
    private RelativeLayout mShouCang;
    private RelativeLayout mCanyu;
    private RelativeLayout mTongzhi;
    private RelativeLayout mCommentMe;
    private RelativeLayout mNvshen;
    private RelativeLayout rlConcerned;
    private RelativeLayout rlFans;
    private ImageView mTuichu;
    private TextView mTuichuText;
    private ImageView mShezhi;
    private ImageView mGender;

    private ImageView mAvatar;
    private TextView mUserName;
    private TextView mUserType;
    private TextView mLevel;
    private TextView mGuanzhuCount;
    private TextView mFensiCount;
    private TextView mTangguoCount;
    private ImageView mBack;
    private ImageView mHuihua;
    private LinearLayout llHeadBg;

    private ImageView chatList;
    private TextView msgJiaobiao;
    private TextView atwoJiaobiao;
    private TextView pinglunJiaobiao;
    private TextView tongzhiJiaobiao;


    private MyInformationBean bean = null;
    private ImageView mAuth;
    /**
     * 用户类型
     */
    private int userAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_information);
        userAuth = getIntent().getIntExtra("userAuth",-1);
        initView();
        initListener();
        initData();
    }

    private void initView() {
        llHeadBg = (LinearLayout) findViewById(R.id.avtarLayout);
        rlConcerned = (RelativeLayout) findViewById(R.id.rl_concerned);
        rlFans = (RelativeLayout) findViewById(R.id.rl_fans);
        mBack = (ImageView) findViewById(R.id.back);
        mHuihua = (ImageView) findViewById(R.id.huihua);
        //chatList = (ImageView) findViewById(R.id.chatList);//去掉了
        mGender = (ImageView) findViewById(R.id.gender);
        mAvatar = (ImageView) findViewById(R.id.avatar);
        mAuth = (ImageView) findViewById(R.id.auth);
        mUserName = (TextView) findViewById(R.id.userName);
        mUserType = (TextView) findViewById(R.id.userType);
        mLevel = (TextView) findViewById(R.id.level);
        mGuanzhuCount = (TextView) findViewById(R.id.guanzhuCount);
        mFensiCount = (TextView) findViewById(R.id.fensiCount);
        mTangguoCount = (TextView) findViewById(R.id.tangguoCount);
        mHome = (RelativeLayout) findViewById(R.id.home);
        mCommentMe = (RelativeLayout) findViewById(R.id.commentMeLayout);
        mMyPhoto = (RelativeLayout) findViewById(R.id.myPhoto);
        //mSugar = (RelativeLayout)findViewById(R.id.sugar);
        mAtMe = (RelativeLayout)findViewById(R.id.atMe);
        mShouCang = (RelativeLayout)findViewById(R.id.shoucang);
        mCanyu = (RelativeLayout)findViewById(R.id.icanyu);
        mTongzhi = (RelativeLayout)findViewById(R.id.tongzhi);
        mTuichu = (ImageView)findViewById(R.id.tuichu);
        mTuichuText = (TextView)findViewById(R.id.tuichuText);
        mShezhi = (ImageView)findViewById(R.id.shezhi);
        mNvshen = (RelativeLayout)findViewById(R.id.nvshen);
        //msgJiaobiao = (TextView)findViewById(R.id.messageJiaobiao);
        atwoJiaobiao = (TextView)findViewById(R.id.atwoJiaobiao);
        pinglunJiaobiao = (TextView)findViewById(R.id.pinglunJiaobiao);
        tongzhiJiaobiao = (TextView)findViewById(R.id.tongzhiJiaobiao);
    }

    @Override
    public void onResume() {
        super.onResume();
        int atmec = SharePreUtils.getInt(this, Constants.ATMES, 0);
        int commentsc = SharePreUtils.getInt(this,Constants.COMMENTMES,0);
        int noticesc = SharePreUtils.getInt(this,Constants.NOTICES,0);
        int messagec = SharePreUtils.getInt(this,Constants.MESSAGES,0);
        if(atmec > 0 && atmec < 99){
            atwoJiaobiao.setVisibility(View.VISIBLE);
            atwoJiaobiao.setText(atmec+"");
        }else if(atmec > 99){
            atwoJiaobiao.setVisibility(View.VISIBLE);
            atwoJiaobiao.setText("99+");
        }else{
            atwoJiaobiao.setVisibility(View.GONE);
        }
        if(commentsc > 0 && commentsc < 99){
            pinglunJiaobiao.setVisibility(View.VISIBLE);
            pinglunJiaobiao.setText(commentsc+"");
        }else if(commentsc > 99){
            pinglunJiaobiao.setVisibility(View.VISIBLE);
            pinglunJiaobiao.setText("99+");
        }else{
            pinglunJiaobiao.setVisibility(View.GONE);
        }
        if(noticesc > 0 && noticesc < 99){
            tongzhiJiaobiao.setVisibility(View.VISIBLE);
            tongzhiJiaobiao.setText(noticesc+"");
        }else if(noticesc > 99){
            tongzhiJiaobiao.setVisibility(View.VISIBLE);
            tongzhiJiaobiao.setText("99+");
        }else{
            tongzhiJiaobiao.setVisibility(View.GONE);
        }
//        if(messagec > 0 && messagec < 99){
//            msgJiaobiao.setVisibility(View.VISIBLE);
//            msgJiaobiao.setText(messagec+"");
//        }else if(noticesc > 99){
//            msgJiaobiao.setVisibility(View.VISIBLE);
//            msgJiaobiao.setText("99+");
//        }else{
//            msgJiaobiao.setVisibility(View.GONE);
//        }
    }

    private void initListener() {
        mHuihua.setOnClickListener(this);
        mBack.setOnClickListener(this);
        //chatList.setOnClickListener(this);去掉了
        mHome.setOnClickListener(this);
        mMyPhoto.setOnClickListener(this);
        // mSugar.setOnClickListener(this);
        mAtMe.setOnClickListener(this);
        mShouCang.setOnClickListener(this);
        mCanyu.setOnClickListener(this);
        mTongzhi.setOnClickListener(this);
        mTuichuText.setOnClickListener(this);
        mTuichu.setOnClickListener(this);
        mShezhi.setOnClickListener(this);
        mCommentMe.setOnClickListener(this);
        mAvatar.setOnClickListener(this);
        mNvshen.setOnClickListener(this);
        rlFans.setOnClickListener(this);
        rlConcerned.setOnClickListener(this);
    }

    private void initData() {
        VolleyTool.get(this, Constants.MY_INFORMATION_URL + "?acc_token=" + UserTools.getUser(this).getAcc_token(),
                null, this, Constants.MY_INFORMATION, MyInformationBean.class);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        int total_count = -1;
        switch (v.getId()) {
            case R.id.home:
                if (bean != null) {
                    intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("bean", bean);
                    intent.putExtras(bundle);
                    intent.setClass(this, MyHomePageActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.myPhoto:
//                if(userAuth == 2){
//                    intent = new Intent();
//                    intent.setClass(this, MyAlbumActivity.class);
//                    startActivity(intent);
//                }else{
//                    showToast("此权限暂未开放!");
//                }
                intent = new Intent();
                intent.setClass(this, MyAlbumActivity.class);
                startActivity(intent);
                break;
//            case R.id.sugar:
//                break;
            case R.id.atMe:
                int atmes = SharePreUtils.getInt(this,Constants.ATMES,0);
                total_count = SharePreUtils.getInt(this,Constants.TOTALCOUNT,0);
                SharePreUtils.putInt(this,Constants.ATMES,0);
                SharePreUtils.putInt(this,Constants.TOTALCOUNT,total_count-atmes);
                intent = new Intent();
                intent.setClass(this, AtMeActivity.class);
                startActivity(intent);
                break;
            case R.id.shoucang:
                intent = new Intent();
                intent.setClass(this, MyCollectionActivity.class);
                startActivity(intent);
                break;
            case R.id.commentMeLayout:
                int comments = SharePreUtils.getInt(this,Constants.COMMENTMES,0);
                total_count = SharePreUtils.getInt(this,Constants.TOTALCOUNT,0);
                SharePreUtils.putInt(this,Constants.COMMENTMES,0);
                SharePreUtils.putInt(this,Constants.TOTALCOUNT,total_count-comments);
                intent = new Intent();
                intent.setClass(this, CommentMeActivity.class);
                startActivity(intent);
                break;
            case R.id.icanyu:
                intent = new Intent();
                intent.setClass(this, IJoinActivity.class);
                startActivity(intent);
                break;
            case R.id.tongzhi:
                int noties = SharePreUtils.getInt(this,Constants.NOTICES,0);
                total_count = SharePreUtils.getInt(this,Constants.TOTALCOUNT,0);
                SharePreUtils.putInt(this,Constants.NOTICES,0);
                SharePreUtils.putInt(this,Constants.TOTALCOUNT,total_count-noties);
                intent = new Intent();
                intent.setClass(this, MyNoticeActivity.class);
                startActivity(intent);
                break;
            case R.id.tuichu:
            case R.id.tuichuText:
                new AlertDialog.Builder(this).setTitle("提醒").setMessage("是否退出?")
                        .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        UserTools.logOut(MyInformationActivity.this);
                        MissQApplication.getInstance().logout(false, new EMCallBack() {
                            @Override
                            public void onSuccess() {
                                Log.e("xxxxx", "sdlfjldsflsfldjk");
                            }

                            @Override
                            public void onError(int i, String s) {

                            }

                            @Override
                            public void onProgress(int i, String s) {

                            }
                        });
                        Intent intent = new Intent();
                        intent.setClass(MyInformationActivity.this, LoginActivity.class);
                        MyInformationActivity.this.startActivity(intent);
                        MyInformationActivity.this.finish();
                    }
                }).show();
                break;
            case R.id.shezhi://设置页面
                intent = new Intent();
                intent.setClass(this, SettingActivity.class);
                startActivity(intent);
                break;
//            case R.id.chatList:
//                intent = new Intent();
//                intent.setClass(this, AuthorSearchActivity.class);
//                startActivity(intent);
//                break;
            case R.id.huihua:
                int messagec = SharePreUtils.getInt(this,Constants.MESSAGES,0);
                total_count = SharePreUtils.getInt(this,Constants.TOTALCOUNT,0);
                SharePreUtils.putInt(this,Constants.MESSAGES,0);
                SharePreUtils.putInt(this,Constants.TOTALCOUNT,total_count-messagec);
                intent = new Intent();
                intent.setClass(this, ChatHistoryActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_fans:
                intent = new Intent();
                intent.setClass(this, MyFansActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_concerned:
                intent = new Intent();
                intent.setClass(this, MyContractorActivity.class);
                intent.putExtra("tag",false);
                startActivity(intent);
                break;
            case R.id.back:
                finish();
                break;
            case R.id.avatar:
                intent = new Intent();
                intent.setClass(this, PersonalInfoActivity.class);
                intent.putExtra("info", bean);
                startActivityForResult(intent, 0);
                break;
            case R.id.nvshen:
                intent = new Intent();
                intent.setClass(this, NvShenActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            initData();
        }
    }

    @Override
    public <T> void onResponse(T t, int requestCode) {
        if (requestCode == Constants.MY_INFORMATION) {
            bean = (MyInformationBean) t;
            mUserName.setText(bean.getData().getNkname());
            switch (bean.getData().getAuth()) {
                case 0:
                    mUserType.setText("普通用户");
                    mAuth.setVisibility(View.GONE);
                    break;
                case 1:
                    mUserType.setText("加V");
                    mAuth.setVisibility(View.GONE);
                    break;
                case 2:
                    mUserType.setText("女神");
                    mAuth.setVisibility(View.VISIBLE);
                    break;
            }
            mLevel.setText("level " + bean.getData().getLevel());
            mGuanzhuCount.setText(bean.getData().getFollows() + "");
            mFensiCount.setText(bean.getData().getFans() + "");
            // mTangguoCount.setText(bean.getData().getEnroll() + "");
            //mTangguoCount.setText(0 + "");
            mTangguoCount.setText(bean.getData().getAccount().getAmountSweet() + "");
            String bgUrl = bean.getData().getBackground();
            if (bgUrl != null && !bgUrl.isEmpty()) {
                VolleyTool.getImageLoader().get(Constants.IMAGE_HOST + bgUrl, new ImageLoader.ImageListener() {
                    @Override
                    public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
                        if (imageContainer != null && imageContainer.getBitmap() != null) {
                            llHeadBg.setBackgroundDrawable(new BitmapDrawable(getResources(), imageContainer.getBitmap()));
                        }
                    }

                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                    }
                });
            }
            int sex = R.mipmap.boy_icon;
            if (bean.getData().getGender() == 1) {
                sex = R.mipmap.boy_icon;
            } else {
                sex = R.mipmap.girl_icon;
            }
            mGender.setImageResource(sex);
            VolleyTool.getImageLoader().get(Constants.IMAGE_HOST + bean.getData().getAvatar(), new ImageLoader.ImageListener() {
                @Override
                public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
                    if (imageContainer.getBitmap() != null) {
                        mAvatar.setImageBitmap(BitmapUtils.toRoundBitmap(imageContainer.getBitmap(), 3, R.color.white));
                    }
                }

                @Override
                public void onErrorResponse(VolleyError volleyError) {

                }
            });
        }
    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {

    }
}
